﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using FluxJpeg.Core;
using FluxJpeg.Core.Encoder;
using USTF.Media.MediaServiceConsumer.USTFMediaServiceProxy;
using USTF.Media.MediaServiceConsumer;
using System.ComponentModel;

namespace SUMO.MediaBrowser.Foundation
{
    public class OpsContext : INotifyPropertyChanged
    {
        public OpsContext()
        {
            //default values
            ServiceBase = "http://umbracoworking.dev/USTFMediaService/USTFMediaUmbracoImages.svc";
            Username = "admin";

            //notify connection changed when any relevant properties have changed.
            //this will enable any part of the app to perform neccessary steps when
            //new connection parameters are introduced (eg: resetting the current data)
            //TODO: add cancellation capabilities!
            this.PropertyChanged += (object sender, PropertyChangedEventArgs e)
                =>
                {
                    switch (e.PropertyName)
                    {
                        case "ServiceBase":
                        case "Username":
                        case "Password":
                            OnConnectionChanged();
                            break;
                    }
                };
        }

        private const string jsDebug =
            @"function _dbg(text)
                {        // VS script debugger output window.
                        if ((typeof(Debug) !== 'undefined') && Debug.writeln) {
                            Debug.writeln(text);
                        }
                        // FF firebug and Safari console.
                        if (window.console && window.console.log) {
                            window.console.log(text);
                        }
                        // Opera console.
                        if (window.opera) {
                            window.opera.postError(text);
                        }
                        // WebDevHelper console.
                        if (window.debugService) {
                            window.debugService.trace(text);
                        }
                };";

        private const string jsDebugExec = "_dbg('{0}');";

        public static OpsContext Current
        {
            get { return Singleton<OpsContext>.Instance; }
        }
        public USTFMediaServiceClient NewClient()
        {
            return Transport.GetClient(Username, Password, ServiceBase);
        }

        public event EventHandler ConnectionChanged;
        public void OnConnectionChanged()
        {
            if (ConnectionChanged != null)
                ConnectionChanged(this, new EventArgs());
        }

        string _serviceUrl;
        public string ServiceBase
        {
            get
            {
                return _serviceUrl;
            }
            set
            {
                if (_serviceUrl == value)
                    return;
                _serviceUrl = value;
                OnPropertyChanged("ServiceBase");
            }
        }


        public string Username { get; set; }

        public string Password { get; set; }

        public static void DebugWrite(string text)
        {
            Debug.WriteLine(text);
            if (!App.Current.IsRunningOutOfBrowser) HtmlPage.Window.Eval(jsDebug +
                                 String.Format(jsDebugExec, (text ?? "").Replace("\\", "\\\\").Replace("'", "\\'")));
        }



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}